package com.example.android1.mytaxi.account.presenter;

import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import com.example.android1.mytaxi.account.model.IAccountManager;
import com.example.android1.mytaxi.account.model.response.SmsCodeResponse;
import com.example.android1.mytaxi.account.model.response.UserExistResponse;
import com.example.android1.mytaxi.account.view.ISmsCodeDialogView;
import com.example.android1.mytaxi.common.databus.RegisterBus;

import java.lang.ref.WeakReference;

/**
 * Created by liuguangli on 17/5/13.
 * Presenter 实现类。
 */

public class SmsCodeDialogPresenterImpl implements ISmsCodeDialogPresenter {
    private ISmsCodeDialogView view; //持有的View
    private IAccountManager accountManager; //持有的Model
    //构造函数
    public SmsCodeDialogPresenterImpl(ISmsCodeDialogView view,
                                      IAccountManager accountManager) {
        this.view = view;
        this.accountManager = accountManager;
    }
//    private static class MyHandler extends Handler{
//        WeakReference<SmsCodeDialogPresenterImpl> refContext;
//        public MyHandler(SmsCodeDialogPresenterImpl context) {
//            this.refContext = new WeakReference<>(context);
//        }
//        @Override
//        public void handleMessage(@NonNull Message msg) {
//            // switch- case: IAccountManager.SMS_SEND_SUC
//        }
//    }
    //mHandler.sendEmptyMessage(IAccountManager.SMS_SEND_SUC);

    @RegisterBus
    public void onSmsCodeResponse(SmsCodeResponse smsCodeResponse) {
        switch (smsCodeResponse.getCode()) {
            case IAccountManager.SMS_SEND_SUC://验证码发成功
                view.showCountDownTimer();
                break;
            case IAccountManager.SMS_SEND_FAIL://验证码发送失败
                view.showError(IAccountManager.SMS_SEND_FAIL, "");
                break;
            case IAccountManager.SMS_CHECK_SUC://验证码校验成功
                view.showSmsCodeCheckState(true);
                break;
            case IAccountManager.SMS_CHECK_FAIL://验证码错误
                view.showError(IAccountManager.SMS_CHECK_FAIL, "");
                break;
            case IAccountManager.SERVER_FAIL://服务器错误
                view.showError(IAccountManager.SERVER_FAIL, "");
                break;
        }
    }

    //自定义注解，用于标记观察者的方法
    @RegisterBus   //用户是否存在
    public void onSmsCodeResponse(UserExistResponse userExistResponse) {
        switch (userExistResponse.getCode()) {
            case IAccountManager.USER_EXIST://用户存在
                view.showUserExist(true);
                break;
            case IAccountManager.USER_NOT_EXIST://用户不存在
               view.showUserExist(false);
                break;
            case IAccountManager.SERVER_FAIL://服务器出错
                view.showError(IAccountManager.SERVER_FAIL, "");
                break;
        }
    }

    /**
     * 获取验证码
     * @param phone
     */
    @Override
    public void requestSendSmsCode(String phone) {
        accountManager.fetchSMSCode(phone);
    }

    /**
     * 验证码这验证码
     * @param phone
     * @param smsCode
     */
    @Override
    public void requestCheckSmsCode(String phone, String smsCode) {
        accountManager.checkSmsCode(phone, smsCode);
    }

    /**
     * 检查用户是否存在
     * @param phone
     */
    @Override
    public void requestCheckUserExist(String phone) {
        accountManager.checkUserExist(phone);
    }

}
